// ignore_for_file: library_private_types_in_public_api, must_be_immutable

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:kmxy_base/widget/divider_kmxy.dart';

class MapSearchView extends StatefulWidget {
  List dataList;
  String name;
  String? title;
  Function onCall;
  Function onCancel;

  MapSearchView(this.dataList, this.name, this.onCall, this.onCancel,
      {this.title, Key? key})
      : super(key: key);

  @override
  _MapSearchViewState createState() => _MapSearchViewState();
}

class _MapSearchViewState extends State<MapSearchView> {
  List dataList = [];
  @override
  void initState() {
    dataList.addAll(widget.dataList);
    setState(() {});
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Expanded(child: Container()),
        Container(
          width: MediaQuery.of(context).size.width,
          height: 61.w,
          decoration: BoxDecoration(
            color: Colors.white,
            // 圆角设置
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(18.w),
              topRight: Radius.circular(18.w),
            ),
          ),
          child: Stack(
            alignment: Alignment.center,
            children: [
              Text(
                widget.title ?? '请选择',
                style: TextStyle(
                    color: const Color(0xff1D2541),
                    fontWeight: FontWeight.bold,
                    fontSize: 16.sp),
              ),
              Positioned(
                right: 0.w,
                child: InkWell(
                  onTap: () {
                    widget.onCancel();
                  },
                  child: Container(
                    padding: EdgeInsets.all(14.w),
                    child: Text(
                      '取消',
                      style: TextStyle(
                          color: const Color(0xff43A7F9), fontSize: 14.sp),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
        Container(
          color: Colors.white,
          child: Container(
            margin: EdgeInsets.symmetric(horizontal: 10.w),
            decoration: BoxDecoration(
              color: const Color(0xffF5F6F7),
              borderRadius: BorderRadius.circular(100.w),
            ),
            child: TextField(
              maxLines: 1,
              textAlignVertical: TextAlignVertical.center,
              decoration: InputDecoration(
                isDense: true,
                fillColor: const Color(0xffF5F6F7),
                border: InputBorder.none,
                constraints: BoxConstraints(maxHeight: 36.w),
                contentPadding:
                    EdgeInsets.only(left: 0, top: 0, bottom: 0, right: 15.w),
                prefixIcon:
                    const Icon(Icons.search_rounded, color: Color(0x991d2541)),
                hintText: "请输入关键词",
                hintStyle: TextStyle(
                  color: const Color(0x991d2541),
                  fontSize: 14.sp,
                ),
              ),
              cursorColor: const Color(0xff1D2541),
              style: TextStyle(
                fontSize: 14.sp,
                color: const Color(0xff1D2541),
              ),
              onChanged: (text) {
                dataList.clear();
                if (text.isEmpty) {
                  for (Map data in widget.dataList) {
                    dataList.add(data);
                  }
                } else {
                  for (Map data in widget.dataList) {
                    if (data[widget.name].contains(text)) {
                      dataList.add(data);
                    }
                  }
                }
                setState(() {});
              },
            ),
          ),
        ),
        Container(
          color: Colors.white,
          padding: EdgeInsets.only(top: 22.w),
          height: 540.w,
          child: SafeArea(
            left: false,
            right: false,
            top: false,
            child: MediaQuery.removePadding(
              context: context,
              removeTop: true,
              child: ListView.separated(
                itemCount: dataList.length,
                itemBuilder: (BuildContext context, int index) {
                  return InkWell(
                    child: Container(
                      padding: EdgeInsets.symmetric(horizontal: 30.w),
                      child: Text(
                        dataList[index][widget.name],
                        style: TextStyle(
                            color: const Color(0xff1D2541), fontSize: 14.sp),
                      ),
                    ),
                    onTap: () {
                      widget.onCall(dataList[index], index);
                    },
                  );
                },
                separatorBuilder: (BuildContext context, int index) {
                  return Container(
                    margin:
                        EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.w),
                    child: const DividerKmxy(height: 0),
                  );
                },
              ),
            ),
          ),
        ),
      ],
    );
  }
}
